# -*- coding: utf-8 -*-
"""
Created on Tue Aug 14 20:43:43 2018

@author: Hy
"""

import requests
from bs4 import BeautifulSoup


def get_html_text(m, url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        with open('err.txt', 'a', encoding='utf-8') as f:
            f.write(m + '\n')
        return None


def get_dates(m, url):
    l = []
    text = get_html_text(m, url)
    if text:
        soup = BeautifulSoup(text, 'lxml')
        datas = soup.select('tr')
        for data in datas:
            l1 = [td.get_text().replace('\n', '') for td in data.select('td')]
            l2 = ['id', 'Source', 'Pair', 'Volume(24h)', 'Price', 'Volume(%)', 'Category', 'FeeType', 'Updated', ]
            if dict(zip(l2, l1)):
                l.append(dict(zip(l2, l1)))
        x = {
            m: l
        }
        with open('code.txt', 'a', encoding='utf-8') as f:
            f.write(str(x) + '\n')


def main():
    with open('币种比较.txt') as f:
        while 1:
            m = f.readline()
            if m:
                m = m.strip().replace(' ', '-')
                url = 'https://coinmarketcap.com/currencies/{}/#markets'.format(m)
                get_dates(m, url)
                print('---{}详情获取中---'.format(m))


main()
